<?
/**
 * sendmail function
 *
 * This fucntion implements the ability to send mail.
 *
 * LICENSE: GPL Version 2
 *
 * @copyright	2006 SANS Institute
 * @license		http://www.gnu.org/copyleft/gpl.html
 * @version 	$id$
 * @since 		File available since Release 1
 */

function sendmail_template($sFile,$aData,$bPGP=FALSE) {
    if ( key_exists('to',$aData) ) {
        $sTo=$aData['to'];
    } else {
        $sTo=ADMINEMAIL;
    }
    if ( key_exists('subject',$aData) ) {
        $sSubject=$aData['subject'];
    } else {
        $sSubject='-no subject-';
    }
    if (key_exists('from',$aData)) {
        $sFrom=$aData['from'];
    } else {
        $sFrom=ADMINEMAIL;
    }
    $sTemplate=file_get_contents(ETC."$sFile",flase);
    foreach ( $aData as $sKey=>$sValue  ) {
        $sTemplate=ereg_replace("%%$sKey%%",$sValue,$sTemplate);
    }
    $sTemplate=ereg_replace("%%[^%]*%%","",$sTemplate);
    $sHeaders="From: $sFrom\r\nBcc: jullrich@euclidian.com";
    $sArgs="-f $sFrom";
    sendmail($sTo,$sSubject,$sTemplate,$sHeaders,$sArgs,$bPGP);
}
function sendmail($sTo,$sSubject,$sText,$sHeaders='',$sArgs='',$bPGP=FALSE)
{
    if ( $sHeaders=='' ) {
        $sHeaders="From: ".FROMEMAIL;
    }
    if ( $sArgs=='' ) {
        $sArgs="-f ".FROMEMAIL;
    }
    applog(LOG_INFO,"sending email to $sTo");
    if ( $bPGP ) {
      $sText=gpgcrypt($sText);
    } 
    // we use mini_sendmail. so extra command lines will not work.
    
    if ( mail($sTo,$sSubject,$sText,$sHeaders,'') ) {
      return;
    }
    applog(LOG_INFO,"sending mail to $sTo Subject $sSubject failed");
}

function gpgcrypt ($sMessage) {
  $sBody = escapeshellarg($sMessage);
  $cmd = "echo $sBody | ".GPG." --quiet --no-secmem-warning --no-permission-warning --homedir=/home/live/isc/.gnupg --quiet --clearsign --armor";
 $sBody = `$cmd`;
 return ($sBody);
}
